package com.guchenbo.example.lock;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreMutex;
import org.apache.curator.framework.recipes.locks.InterProcessSemaphoreV2;
import org.apache.curator.framework.recipes.locks.Lease;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.junit.jupiter.api.Test;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class CuratorLockDemo {

    private static ExecutorService executorService = Executors.newFixedThreadPool(10);

    public static void main(String[] args) throws InterruptedException {
        // 创建链接
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy);
        client.start();
        // 创建锁对象
        CuratorLock1 mutex = new CuratorLock1(client, "/gcb/lock/mylock");
        for (int i = 0; i < 5; i++) {
            int finalI = i;
            executorService.submit(() -> {
                try {
                    System.out.println("start");
                    // 加锁
                    mutex.acquire();
                    System.out.println(mutex.getLockPath());
                    System.out.println("lock success");
                    //                                    Thread.sleep(3000);
                    new WaitClientCloseServer().start(10000 + finalI);
                    // 释放锁
                    mutex.release();
                    System.out.println("end");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
        }
        new CountDownLatch(1).await();
    }

    @Test
    public void curatorTest() throws Exception {

        // 创建链接
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy);
        client.start();
        // 创建 Semaphore
        InterProcessSemaphoreV2 v2 = new InterProcessSemaphoreV2(client, "/gcb/lock/semaphor", 1);
        for (int i = 0; i < 5; i++) {
            int finalI = i;
            executorService.submit(() -> {
                try {
                    System.out.println("start");
                    // 加锁
                    Lease lease = v2.acquire();
                    //                  System.out.println(mutex.getLockPath());
                    System.out.println("lock success");
                    //                                    Thread.sleep(3000);
                    new WaitClientCloseServer().start(10000 + finalI);
                    // 释放锁
                    //                  mutex.release();
                    v2.returnLease(lease);
                    System.out.println("end");
                } catch (Exception e) {
                    throw new RuntimeException(e);
                }
            });
        }
        new CountDownLatch(1).await();

    }

    @Test
    public void semaphoreMutex() throws Exception {

        // 创建链接
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
        CuratorFramework client = CuratorFrameworkFactory.newClient("localhost:2181", retryPolicy);
        client.start();
        // 创建 Semaphore
        InterProcessSemaphoreMutex lock = new InterProcessSemaphoreMutex(client, "/gcb/lock/semaphor");
        for (int i = 0; i < 5; i++) {
            int finalI = i;
            lock.acquire();
            System.out.println("lock");
            lock.acquire();
            System.out.println("lock again");
            lock.release();
            lock.release();
            //            executorService.submit(() -> {
            //                try {
            //                    System.out.println("start");
            //                    // 加锁
            //                    lock.acquire();
            //                    //                  System.out.println(mutex.getLockPath());
            //                    System.out.println("lock success");
            //                    //                                    Thread.sleep(3000);
            //                    new WaitClientCloseServer().start(10000 + finalI);
            //                    // 释放锁
            //                    //                  mutex.release();
            //                    lock.release();
            //                    System.out.println("end");
            //                } catch (Exception e) {
            //                    throw new RuntimeException(e);
            //                }
            //            });
        }
        new CountDownLatch(1).await();

    }

}
